#!/usr/bin/env bash

REALPATH=$(realpath $0)
WORK_DIR=$(cd $(dirname $REALPATH)/.. && pwd)

# Get the metadata of machine.
if [[ -f ${WORK_DIR}/.env ]]; then source ${WORK_DIR}/.env; fi
echo "LoadEnv CLOUD=$CLOUD REGION=$REGION, SOURCE=$SOURCE"

# For CentOS 7, it's /etc/redis.conf
# For Ubuntu 20, it's /etc/redis/redis.conf
REDIS_CONF=/etc/redis.conf
if [[ ! -f $REDIS_CONF ]]; then REDIS_CONF=/etc/redis/redis.conf; fi
if [[ ! -f $REDIS_CONF ]]; then echo "no redis config"; fi

# If redis also listen at 56379, stop it.
grep -q "^port 56379" $REDIS_CONF 2>/dev/null; SRS_REDIS_EXISTS=$?
systemctl status redis >/dev/null 2>&1; REDIS_ALIVE=$?
if [[ $SRS_REDIS_EXISTS -eq 0 && $REDIS_ALIVE -eq 0 ]]; then
  systemctl stop redis; if [[ $? -ne 0 ]]; then echo "Stop redis failed"; exit 1; fi
fi

# Compatible with previous redis. Note that we should copy data after stop redis, which save data to disk.
if [[ $SRS_REDIS_EXISTS -eq 0 ]]; then
  if [[ ! -f ${WORK_DIR}/containers/data/redis/dump.rdb && -f /var/lib/redis/dump.rdb ]]; then
    mkdir -p ${WORK_DIR}/containers/data/redis && cp /var/lib/redis/dump.rdb ${WORK_DIR}/containers/data/redis/dump.rdb
    if [[ $? -ne 0 ]]; then echo "Copy redis database failed"; exit 1; fi
  fi
fi

# Always write all cli tools to /root
# Note that it's not required tool, so we only need a place to save the script.
if [[ $CLOUD != 'BT' ]]; then
  REDIS_CLI=/root/redis-cli
  API_SECRET_CLI=/root/api-secret
fi
echo "Setup redis script, REDIS_CONF=${REDIS_CONF}, REDIS_CLI=${REDIS_CLI}, API_SECRET_CLI=${API_SECRET_CLI}"

# Save host to .env
grep -q "^REDIS_HOST=" ${WORK_DIR}/.env; REDIS_HOST=$?
if [[ $REDIS_HOST -ne 0 ]]; then
  echo "REDIS_HOST=redis" >> ${WORK_DIR}/.env && echo "Setup REDIS_HOST=redis ok"
  if [[ $? -ne 0 ]]; then echo "Setup REDIS_HOST failed"; exit 1; fi
fi

# Save port to .env
grep -q "^REDIS_PORT=" ${WORK_DIR}/.env; SETUP_PORT=$?
if [[ $SETUP_PORT -ne 0 ]]; then
  echo "REDIS_PORT=56379" >> ${WORK_DIR}/.env && echo "Setup REDIS_PORT=56379 ok"
  if [[ $? -ne 0 ]]; then echo "Setup REDIS_PORT failed"; exit 1; fi
fi

# Save password to .env
grep -q "^REDIS_PASSWORD=" ${WORK_DIR}/.env; SETUP_PASSWORD=$?
if [[ $SETUP_PASSWORD -ne 0 ]]; then
  REDIS_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 32)
  echo "REDIS_PASSWORD=${REDIS_PASSWORD}" >> ${WORK_DIR}/.env && echo "Setup REDIS_PASSWORD ok"
  if [[ $? -ne 0 ]]; then echo "Setup REDIS_PASSWORD failed"; exit 1; fi
fi

if [[ ! -z $REDIS_CLI ]]; then
  echo '#!/usr/bin/env bash' > $REDIS_CLI &&
  cat ${WORK_DIR}/.env |grep REDIS >> $REDIS_CLI &&
  echo "redis-cli -p \$REDIS_PORT -a \$REDIS_PASSWORD \$*" >> $REDIS_CLI &&
  chmod +x $REDIS_CLI && echo "Create redis-cli helper at $REDIS_CLI"
fi

if [[ ! -z $API_SECRET_CLI ]]; then
  echo '#!/usr/bin/env bash' > $API_SECRET_CLI &&
  cat ${WORK_DIR}/.env |grep REDIS >> $API_SECRET_CLI &&
  echo "redis-cli -p \$REDIS_PORT -a \$REDIS_PASSWORD \$* HGET SRS_PLATFORM_SECRET token" >> $API_SECRET_CLI &&
  chmod +x $API_SECRET_CLI && echo "Create api-secret helper at $API_SECRET_CLI"
fi

